﻿using System;
using System.Windows.Forms;
using ApplicationGenerator.CommonModule;
using ApplicationGenerator.Versions.BusinessLogic;
using ApplicationGenerator.Versions.Forms;

namespace ApplicationGenerator.GeneratorMenu.Forms
{
    /// <summary>
    /// Formatka wyświetlająca kontrolkę do konfiguracji elementu menu
    /// </summary>
    public partial class UrlSelectForm : Form
    {
        private static readonly string _NoneText = "<BRAK>";

        public string SelectedUrl
        {
            get;
            set;
        }

        public UrlSelectForm()
        {
            InitializeComponent();
        }

        private void ctlSelectButton_Click(object sender, EventArgs e)
        {
            VersionElement versionType = GetVersionElement();
            VersionForm versionForm = new VersionForm();
            versionForm.VersionType = versionType;
            versionForm.InitializeVersions();

            if (versionForm.ShowDialog() == DialogResult.OK)
            {
                ctlElementInfo.Tag = versionForm.SelectedVersionInfo;
                ctlElementInfo.Text = versionForm.SelectedVersionInfo.Name;
            }
        }

        private VersionElement GetVersionElement()
        {
            if (ctlFormRadio.Checked)
            {
                return VersionElement.Form;
            }

            if (ctlListRadio.Checked)
            {
                return VersionElement.List;
            }

            if (ctlWorkflowRadio.Checked)
            {
                return VersionElement.Workflow;
            }

            throw new NotImplementedException("Brak innej możliwości");
        }

        private void ctlOk_Click(object sender, EventArgs e)
        {
            if (ctlCustomRadio.Checked)
            {
                if (string.IsNullOrEmpty(ctlCustomPage.Text))
                {
                    MessageBox.Show("Należy podać url strony");
                    return;
                }

                SelectedUrl = ctlCustomPage.Text;
                CloseOk();
                return;
            }

            if (ctlElementInfo.Tag == null || !(ctlElementInfo.Tag is VersionInfo))
            {
                MessageBox.Show("Należy wybrać element do wyświetlenia");
                return;
            }

            VersionInfo versionInfo = ctlElementInfo.Tag as VersionInfo;

            if (ctlFormRadio.Checked)
            {
                SelectedUrl = QuerryStringConstants.GenerateNewElementFormUrl(versionInfo.Version.ToString());
            }

            if (ctlListRadio.Checked)
            {
                SelectedUrl = QuerryStringConstants.GenerateListUrl(versionInfo.Version.ToString());
            }

            if (ctlWorkflowRadio.Checked)
            {
                SelectedUrl = QuerryStringConstants.GenerateNewWorkflowUrl(versionInfo.Version.ToString());
            }

            CloseOk();

        }

        private void CloseOk()
        {
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void ctlCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void ctlFormRadio_CheckedChanged(object sender, EventArgs e)
        {
            DisableCustomSelect();
        }

        private void ctlWorkflowRadio_CheckedChanged(object sender, EventArgs e)
        {
            DisableCustomSelect();
        }

        private void ctlListRadio_CheckedChanged(object sender, EventArgs e)
        {
            DisableCustomSelect();
        }

        private void DisableCustomSelect()
        {
            ctlElementInfo.Text = _NoneText;
            ctlCustomPage.Text = string.Empty;
            ctlCustomPage.Enabled = false;
            ctlSelectButton.Enabled = true;
        }

        private void ctlCustomRadio_CheckedChanged(object sender, EventArgs e)
        {
            ctlElementInfo.Text = _NoneText;
            ctlCustomPage.Enabled = true;
            ctlSelectButton.Enabled = false;
        }
    }
}
